package com.ossjk.ossjkservlet.system.servlet;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import com.ossjk.core.base.servlet.BaseServlet;
import com.ossjk.core.util.CommonUtil;
import com.ossjk.core.vo.AjaxReturn;
import com.ossjk.core.vo.Page;
import com.ossjk.ossjkservlet.common.Constant;
import com.ossjk.ossjkservlet.system.dao.OrdergoodsDao;
import com.ossjk.ossjkservlet.system.dao.OrdersDao;
import com.ossjk.ossjkservlet.system.pojo.Ordergoods;
import com.ossjk.ossjkservlet.system.pojo.Orders;

@WebServlet("/system/orders.do")
public class OrdersServlet extends BaseServlet {

	private Logger logger = Logger.getLogger(OrdersServlet.class);
	private OrdersDao ordersDao = new OrdersDao();
	private OrdergoodsDao ordergoodsDao = new OrdergoodsDao();
	

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) {
		doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) {
		String method = getMethod(req);
		if ("list".equals(method)) {
			list(req, resp);
		} else if ("toUpdate".equals(method)) {
			toUpdate(req, resp);
		} else if ("update".equals(method)) {
			update(req, resp);
		} else if ("selOne".equals(method)) {
			selectOne(req, resp);
		}
	}
	
	/**
	 * 查看订单详情
	 * @param req
	 * @param resp
	 */
	private void selectOne(HttpServletRequest req, HttpServletResponse resp) {
		
		String id = req.getParameter("id");
		
		try {
			Page page = getParamtoObject(Page.class, req);
			String ogsql = "select * from ordergoods where oid = ? ";
			List<Ordergoods> og =  ordergoodsDao.selectSome(ogsql, id);
			req.setAttribute("orderGoods", og);
			
			String sql = "select o.*,c.name as cname from orders o LEFT JOIN customer c on o.cid = c.id";
			List args = new ArrayList();
			if (!CommonUtil.isBlank(req.getParameter("id"))) {
				sql += " where o.id = ? ";
				args.add(req.getParameter("id"));
				page.getPrams().put("name", req.getParameter("id"));
			}
			req.setAttribute("page", ordersDao.selectByPageCount(sql, page, args.toArray()));
			// 转发
			forward(req, resp, "/WEB-INF/page/system/orders/list.jsp");
		} catch (Exception e) {
			logger.error("", e);
			// 重定向
			redirect(req, resp, req.getContextPath() + "/system/index.do?method=toError");
		}
		
	}

	/**
	 * 列表
	 * 
	 * @param req
	 * @param resp
	 */
	public void list(HttpServletRequest req, HttpServletResponse resp) {
		try {
			Page page = getParamtoObject(Page.class, req);
			String sql = "select o.*,c.name as cname from orders o LEFT JOIN customer c on o.cid = c.id";
			//String sql = ordersDao.getAllSql();
			//String sql = "select * from orders left outer JOIN customer on orders.cid=customer.id";
			//sql += " where 1 = 1 ";
			List args = new ArrayList();
			if (!CommonUtil.isBlank(req.getParameter("id"))) {
				sql += " where o.id = ? ";
//				sql += " where  o.id like  CONCAT('%',?,'%') ";
				//sql += "and name like  CONCAT('%',?,'%')";
				args.add(req.getParameter("id"));
				page.getPrams().put("name", req.getParameter("id"));
			}
			req.setAttribute("page", ordersDao.selectByPageCount(sql, page, args.toArray()));
			// 转发
			forward(req, resp, "/WEB-INF/page/system/orders/list.jsp");
		} catch (Exception e) {
			logger.error("", e);
			// 重定向
			redirect(req, resp, req.getContextPath() + "/system/index.do?method=toError");
		}

	}

	/**
	 * 去增加
	 * 
	 * @param req
	 * @param resp
	 */
	public void toInsert(HttpServletRequest req, HttpServletResponse resp) {
		try {
			forward(req, resp, "/WEB-INF/page/system/orders/edit.jsp");
		} catch (Exception e) {
			logger.error("", e);
			redirect(req, resp, req.getContextPath() + "/system/index.do?method=toError");
		}
	}

	/**
	 * 去更新
	 * 
	 * @param req
	 * @param resp
	 */
	public void toUpdate(HttpServletRequest req, HttpServletResponse resp) {
		try {
			req.setAttribute("record", ordersDao.selectById(req.getParameter("id")));
			forward(req, resp, "/WEB-INF/page/system/orders/edit.jsp");
		} catch (Exception e) {
			logger.error("", e);
			redirect(req, resp, req.getContextPath() + "/system/index.do?method=toError");
		}
	}

	/**
	 * 增加
	 * 
	 * @param req
	 * @param resp
	 * @throws Exception
	 */
	public void insert(HttpServletRequest req, HttpServletResponse resp) {
		
		try {
			// getParamtoObject
			// 假设表单和实体类字段一致，可以调用getParamtoObject方法把表单内容自动封装到实体类当中
			Orders orders = getParamtoObject(Orders.class, req);
			
			UUID uuid = UUID.randomUUID();
			String id = uuid.toString();
			id = id.replaceAll("-", "");
			orders.setId(id);
			if (ordersDao.insert(orders) > 0) {
				renderAjaxReturn(resp, new AjaxReturn(Constant.RETURN_CODE_SUCCESS, Constant.RETURN_MESSAGE_SUCCESS));
			} else {
				renderAjaxReturn(resp, new AjaxReturn(Constant.RETURN_CODE_ERROR, Constant.RETURN_MESSAGE_ERROR));
			}
		} catch (Exception e) {
			logger.error("", e);
			renderAjaxReturn(resp, new AjaxReturn(Constant.RETURN_CODE_EXCEPTION, Constant.RETURN_MESSAGE_EXCEPTION));
		}
	}

	/**
	 * 更新
	 * 
	 * @param req
	 * @param resp
	 * @throws Exception
	 */
	public void update(HttpServletRequest req, HttpServletResponse resp) {
		try {
			Orders orders = getParamtoObject(Orders.class, req);
			
			if (ordersDao.updateBySelected(orders) > 0) {
				renderAjaxReturn(resp, new AjaxReturn(Constant.RETURN_CODE_SUCCESS, Constant.RETURN_MESSAGE_SUCCESS));
			} else {
				renderAjaxReturn(resp, new AjaxReturn(Constant.RETURN_CODE_ERROR, Constant.RETURN_MESSAGE_ERROR));
			}
		} catch (Exception e) {
			logger.error("", e);
			renderAjaxReturn(resp, new AjaxReturn(Constant.RETURN_CODE_EXCEPTION, Constant.RETURN_MESSAGE_EXCEPTION));
		}
	}

	/**
	 * 删除
	 * 
	 * @param req
	 * @param resp
	 */
	public void delete(HttpServletRequest req, HttpServletResponse resp) {
		try {
			if (ordersDao.delete(req.getParameter("id")) > 0) {
				renderAjaxReturn(resp, new AjaxReturn(Constant.RETURN_CODE_SUCCESS, Constant.RETURN_MESSAGE_SUCCESS));
			} else {
				renderAjaxReturn(resp, new AjaxReturn(Constant.RETURN_CODE_ERROR, Constant.RETURN_MESSAGE_ERROR));
			}
		} catch (Exception e) {
			logger.error("", e);
			renderAjaxReturn(resp, new AjaxReturn(Constant.RETURN_CODE_EXCEPTION, Constant.RETURN_MESSAGE_EXCEPTION));
		}
	}

	/**
	 * 批量删除
	 * 
	 * @param req
	 * @param resp
	 */
	public void batchDelete(HttpServletRequest req, HttpServletResponse resp) {
		try {
			System.out.println("======0000:" + req.getParameterValues("ids"));
			if (ordersDao.batchDelete(req.getParameterValues("ids")) > 0) {
				renderAjaxReturn(resp, new AjaxReturn(Constant.RETURN_CODE_SUCCESS, Constant.RETURN_MESSAGE_SUCCESS));
			} else {
				renderAjaxReturn(resp, new AjaxReturn(Constant.RETURN_CODE_ERROR, Constant.RETURN_MESSAGE_ERROR));
			}
		} catch (Exception e) {
			logger.error("", e);
			renderAjaxReturn(resp, new AjaxReturn(Constant.RETURN_CODE_EXCEPTION, Constant.RETURN_MESSAGE_EXCEPTION));
		}
	}

}
